#include "rfid_task.h"
#include "buzzer.h"
#include "sg90.h"
#include "oled.h"

#if DEBUG

#include "debug_uart.h"

#endif





extern uint8_t data_len;//接受缓冲去长度
extern uint8_t recv_buf[32];
uint8_t uid_handle_flag=0;
uint8_t uid_weigen_flag=0;
 /*
  * @brief  rfid_task_init rfid读卡功能函数初始化
  * @param  None
  * @retval None
  */
void rfid_task_init(void)
{
	/*  RFID串口初始化  */
	rfid_uart_init();
	
	
	/* rfid 定时器初始化 */
	rfid_timer_init();
	
	/* 韦根初始化 */
	weigen_init();
  
  //标识初始化成功
  oled_rfid_mode(0);
}


 /*
  * @brief  rfid_task读卡任务函数
  * @param  None
  * @retval None
  */
void rfid_task(void)
{
	uint8_t uid[4];
	int8_t status;
	#if DEBUG
	uint8_t i;
	#endif
		//读卡
		status = rfid_getcard(uid);
		if(status==0)
		{
			if(rfid_judge_data_feasible())
			{
				//处理读到uid
				#if DEBUG
				UsartPrintf(USART_DEBUG,"uid:");
				for(i=0;i<4;i++)
				{
					UsartPrintf(USART_DEBUG,"%02x ",uid[i]);
				}
				UsartPrintf(USART_DEBUG,"\r\n");
				#endif
				rfid_uid_handle(uid);
			}
		}
}

 /*
  * @brief  rfid_getcard 获取卡片uid
	* @param  uid:存放uid的数组
  * @retval 1 uid成功获取，0 uid获取失败
  */
int8_t rfid_getcard(uint8_t *uid)
{
	uint8_t i;
	int8_t status;
	rfid_uart_sendcom(4);
	delay_ms(10);
	rfid_uart_sendcom(4);
	status = rfid_uart_sendcom(1);
	if(status == 1)
	{
		for(i=0;i<4;i++)
		{
			uid[i] = recv_buf[i+5];
		}
		rfid_uart_sendcom(4);
		return 0;
	}
	return 1;
}


void rfid_uid_handle(uint8_t *uid)
{
	uint8_t uid_exist_flag = 0;
	uint8_t page_num,uid_flash_num;
	//判断韦根模式
	if(uid_weigen_flag)
	{
		weigen26_out(uid);
		return;
	}
	
	/* 判断uid是否存入flash */
	uid_exist_flag = uid_flash_exist(uid,&page_num,&uid_flash_num);
	
	/* 判断是否读卡的状态模式 */
	switch(uid_handle_flag)
	{
		case 0://普通状态，判断
		{
			if(uid_exist_flag)//存在
			{
        #if DEBUG
        
        UsartPrintf(USART_DEBUG,"unlock\r\n");
        
        #endif
        
				
				SUCCESS_ON_LOCK();
			}
			else
			{
        #if DEBUG
        
        UsartPrintf(USART_DEBUG,"uid no extist\r\n");
        
        #endif
        
				
				si12_fail();
			}
			break;
		}
		
		case 1://加卡状态
		{
			if(uid_exist_flag)//存在
			{
        #if DEBUG
        
        UsartPrintf(USART_DEBUG,"RFID IC uid already exist!!!\r\n");
        
        #endif
        
				
				si12_fail();
			}
			else
			{
				if(add_uid(uid))
				{
          #if DEBUG
          
          UsartPrintf(USART_DEBUG,"add ok!\r\n");
          
          #endif
          
					
					si12_succced();
				}
				
			}
			break;
		}
		
		case 2://减卡状态
		{
			if(uid_exist_flag)//存在
			{
				if(sub_uid(page_num,uid_flash_num))
				{
          #if DEBUG
          
          UsartPrintf(USART_DEBUG,"sub ok!\r\n");
          
          #endif
          
					
					si12_succced();
				}
			}
			else
			{
        #if DEBUG
        
        UsartPrintf(USART_DEBUG,"RFID IC uid inexistence!!!\r\n");
        
        #endif
        
				
				si12_fail();
			}
			break;
		}
	}
}

//判断数据是否干扰，数据是否有效 0 数据无效
uint8_t rfid_judge_data_feasible(void)
{
	uint8_t i,bcc = 0;
	//帧头或帧尾无效
	if(recv_buf[0] != 0xAA || recv_buf[data_len - 2] != 0xBB)
	{
		return 0;
	}
	for(i=0;i< data_len-1;i++)
	{
		bcc ^= recv_buf[i];
	}
	if(recv_buf[data_len-1] != bcc) return 0;
	return 1;
	
}

